package org.example.managere.service.feibiao.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.example.managere.dto.feibiao.AmAapModelInfoDto;
import org.example.managere.entity.feibiao.AmAapModelInfo;
import org.example.managere.repository.feibiao.AmAapModelInfoRepository;
import org.example.managere.service.feibiao.AmAapModelInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * 模型融合配置表Service实现类
 */
@Service
@RequiredArgsConstructor
public class AmAapModelInfoServiceImpl implements AmAapModelInfoService {

    private final AmAapModelInfoRepository repository;
    private final ObjectMapper objectMapper;

    @Override
    public Page<AmAapModelInfoDto> findAll(Pageable pageable) {
        return repository.findAll(pageable).map(this::convertToDto);
    }

    @Override
    public AmAapModelInfoDto findById(Integer id) {
        return repository.findById(id)
                .map(this::convertToDto)
                .orElseThrow(() -> new EntityNotFoundException("模型融合配置不存在"));
    }

    @Override
    @Transactional
    public AmAapModelInfoDto save(AmAapModelInfoDto dto) {
        validateWeights(dto);
        AmAapModelInfo entity = convertToEntity(dto);
        return convertToDto(repository.save(entity));
    }

    @Override
    @Transactional
    public AmAapModelInfoDto update(Integer id, AmAapModelInfoDto dto) {
        validateWeights(dto);
        AmAapModelInfo entity = repository.findById(id)
                .orElseThrow(() -> new EntityNotFoundException("模型融合配置不存在"));
        BeanUtils.copyProperties(dto, entity, "id");
        return convertToDto(repository.save(entity));
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        if (!repository.existsById(id)) {
            throw new EntityNotFoundException("模型融合配置不存在");
        }
        repository.deleteById(id);
    }

    @Override
    public Page<AmAapModelInfoDto> search(String keyword, Pageable pageable) {
        return repository.search(keyword, pageable).map(this::convertToDto);
    }

    /**
     * 验证权重之和是否为1
     */
    private void validateWeights(AmAapModelInfoDto dto) {
        BigDecimal sum = dto.getPhysicalModelWeight().add(dto.getLlmModelWeight());
        if (sum.compareTo(BigDecimal.ONE) != 0) {
            throw new IllegalArgumentException("物理模型权重和大语言模型权重之和必须为1");
        }
    }

    /**
     * 将实体转换为DTO
     */
    private AmAapModelInfoDto convertToDto(AmAapModelInfo entity) {
        AmAapModelInfoDto dto = new AmAapModelInfoDto();
        BeanUtils.copyProperties(entity, dto);
        return dto;
    }

    /**
     * 将DTO转换为实体
     */
    private AmAapModelInfo convertToEntity(AmAapModelInfoDto dto) {
        AmAapModelInfo entity = new AmAapModelInfo();
        BeanUtils.copyProperties(dto, entity);
        return entity;
    }
} 